﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace Jizsoft.JFrame.Modeling.Domain
{
    public class JFDbContext : DbContext
    {
        static JFDbContext()
        {
            Database.SetInitializer<JFDbContext>(null);
        }

        public JFDbContext()
            : base("name=jframe-db")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Entities.DataType>().ToTable("JF_T_DataType");
            modelBuilder.Entity<Entities.DataType>().HasKey(t => t.Id);

            modelBuilder.Entity<Entities.FieldType>().ToTable("JF_T_FieldType");
            modelBuilder.Entity<Entities.FieldType>().HasKey(t => t.Id);

            modelBuilder.Entity<Entities.FieldTypeCategory>().ToTable("JF_T_FieldTypeCategory");
            modelBuilder.Entity<Entities.FieldTypeCategory>().HasKey(t => t.Id);

            modelBuilder.Entity<Entities.Model>().ToTable("JF_T_Model");
            modelBuilder.Entity<Entities.Model>().HasKey(t => t.Id);
            modelBuilder.Entity<Entities.ModelField>().ToTable("JF_T_ModelField");
            modelBuilder.Entity<Entities.ModelField>().HasKey(t => t.Id);
            modelBuilder.Entity<Entities.ModelRelation>().ToTable("JF_T_ModelRelation");
            modelBuilder.Entity<Entities.ModelRelation>().HasKey(t => new { t.Model1Id, t.Model2Id });

            modelBuilder.Entity<Views.ModelRelationDetail>().ToTable("JF_V_ModelRelationDetail");
            modelBuilder.Entity<Views.ModelRelationDetail>().HasKey(t => new { t.Model1Id, t.Model2Id });

            base.OnModelCreating(modelBuilder);
        }
    }
}
